package lib.bone.nepudata.service.impl;

import cn.hutool.core.lang.Validator;
import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.nepudata.dao.NepuAwardDAO;
import lib.bone.nepudata.domain.entity.NepuAward;

import lib.bone.nepudata.expose.WorkloadService;
import lib.bone.nepudata.service.ResearchService;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @Author : kindear
 */

/**
 * cg
 */
@Slf4j
@Service
public class NepuAwardServiceImpl implements ResearchService<NepuAward,String> {

    @Autowired
    NepuAwardDAO nepuAwardDAO;

    @Autowired(required = false)
    WorkloadService workloadService;


//    public Optional<NepuAward> findByAwardNameAndLeader(String awardName,String leader){
//        return nepuAwardDAO.findByAwardNameAndLeader(awardName,leader);
//    }


    @Override
    public String getProjectCode(String v) {
        return "无";
    }

    @Override
    public String getServiceType() {
        return ResearchTypeConst.AWARD;
    }

    @Override
    public List<NepuAward> getByIdIn(List<String> ids) {
        return nepuAwardDAO.findByIdIn(ids);
    }

    @Override
    public NepuAward saveOne(NepuAward nepuAward) {
        if (Validator.isNotEmpty(nepuAward.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return nepuAwardDAO.save(nepuAward);
    }

    @Override
    public NepuAward updateOne(NepuAward nepuAward) {
        if (Validator.isEmpty(nepuAward.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return nepuAwardDAO.save(nepuAward);
    }

    @Override
    public PageResultDTO<NepuAward> getAll(Pageable pageable, Example<NepuAward> example) {
        Page<NepuAward> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = nepuAwardDAO.findAll(pageable);
        }else {
            page = nepuAwardDAO.findAll(example,pageable);
        }
        List<NepuAward> list = page.getContent();
        PageResultDTO<NepuAward> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());

        return pageResultDTO;
    }

    @Override
    public ResponseDTO deleteById(String id) {
        Integer useCount = workloadService.countByLinkId(id);
        // Long useCount = nepuResearchWorkloadDAO.countByResearchTypeAndMapperId("教学科研获奖",id);
        if(useCount > 0){
            return ResponseDTO.wrap(ResponseCodeConst.FORBIDDEN,"存在关联该奖励的科研工作量信息,拒绝执行删除!");
        }
        //判断数据是否存在
        Optional<NepuAward> nepuAwardOp = nepuAwardDAO.findById(id);
        if(!nepuAwardOp.isPresent()) return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"ID:["+id+"]对应数据不存在");
        nepuAwardDAO.deleteById(id);
        return ResponseDTO.succMsg("删除成功!");
    }

    @Override
    public Optional<NepuAward> getById(String id) {
        return nepuAwardDAO.findById(id);
//        return nepuAwardOp;
    }

//    @Override
//    public ResponseDTO<NepuAward> saveOne(NepuAwardAddDTO nepuAwardAddDTO) {
//        NepuAward nepuAward = new NepuAward();
//        Optional<NepuAwardDict> dictOp = dictService.getById(nepuAwardAddDTO.getAwardDictId());
//        if (!dictOp.isPresent()){
//            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"词典ID["+nepuAwardAddDTO.getAwardDictId()+"]对应资源不存在");
//        }
//        NepuAwardDict dict = dictOp.get();
//        BeanUtil.copyProperties(nepuAwardAddDTO,nepuAward, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
//        nepuAward.setWorkload(dict.getWorkloadMax());
//        /** 查询是否已经存在对应数据 **/
////        Example<NepuAward> example = Example.of(nepuAward);
////        List<NepuAward> nepuAwardList = nepuAwardDAO.findAll(example);
////        if(nepuAwardList.size() > 0){
////            return ResponseDTO.wrap(ResponseCodeConst.FORBIDDEN,"相同记录已存在,拒绝新建请求");
////        }
//        return ResponseDTO.succData(nepuAwardDAO.save(nepuAward));
//    }
//
//    @Override
//    public ResponseDTO<NepuAward> updateOne(NepuAwardUpdateDTO nepuAwardUpdateDTO) {
//        Optional<NepuAward> nepuAwardOp = getById(nepuAwardUpdateDTO.getId());
//        if(!nepuAwardOp.isPresent()) return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"ID:["+
//                nepuAwardUpdateDTO.getId()+"]对应的教学科研奖励不存在");
//        NepuAward nepuAward = nepuAwardOp.get();
//        BeanUtil.copyProperties(nepuAwardUpdateDTO,nepuAward,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
//        return ResponseDTO.succData(nepuAwardDAO.save(nepuAward));
//    }

    //    @Override
//    public ResponseDTO<List<NepuAward>> getAll() {
//        return ResponseDTO.succData(nepuAwardDAO.findAll());
//    }
//
//    @Override
//    public ResponseDTO deleteById(String id) {
//        Optional<NepuAward> optional = nepuAwardDAO.findById(id);
//        if(!optional.isPresent())
//            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"ID:["+id+"]对应数据不存在");
//        nepuAwardDAO.deleteById(id);
//        return ResponseDTO.succMsg("删除成功");
//    }
//
//    @Override
//    public ResponseDTO save(NepuAward nepuAward) {
//        return ResponseDTO.succData(nepuAwardDAO.save(nepuAward));
//    }
}
